package zzh.com.kangkenuo.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

/**
 * @author zzh
 * @Name:
 * @Description:
 * @date
 * @time
 * @copyright
 */
public class AMapLocation implements AMapLocationListener {

    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = null;
    private Intent alarmIntent = null;
    private PendingIntent alarmPi = null;
    private AlarmManager alarm = null;
    private com.amap.api.location.AMapLocation amapLocation;
    private getLocation location;

    public static AMapLocation aMapLocation;

    public static AMapLocation getaMapLocation() {
        if (aMapLocation == null)
            aMapLocation = new AMapLocation();
        return aMapLocation;
    }

    public void lodingLocation(Context context, getLocation mLocation) {
        locationClient = new AMapLocationClient(context);
        locationOption = new AMapLocationClientOption();
        // 设置定位模式为高精度模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        // 设置定位监听
        locationClient.setLocationListener(this);
        // 创建Intent对象，action为LOCATION
        alarmIntent = new Intent();
        alarmIntent.setAction("LOCATION");
        IntentFilter ift = new IntentFilter();
        // 定义一个PendingIntent对象，PendingIntent.getBroadcast包含了sendBroadcast的动作。
        // 也就是发送了action 为"LOCATION"的intent
        alarmPi = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
        // AlarmManager对象,注意这里并不是new一个对象，Alarmmanager为系统级服务
        alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        //动态注册一个广播
        IntentFilter filter = new IntentFilter();
        filter.addAction("LOCATION");
        context.registerReceiver(alarmReceiver, filter);
        initOption();
        // 设置定位参数
        locationClient.setLocationOption(locationOption);
        // 启动定位
        locationClient.startLocation();
        mHandler.sendEmptyMessage(Utils.MSG_LOCATION_START);
        if (null != alarm) {
            //设置一个闹钟，2秒之后每隔一段时间执行启动一次定位程序
            alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 2 * 1000,
                    1 * 1000, alarmPi);
        }
        this.location = mLocation;
    }

    Handler mHandler = new Handler() {
        public void dispatchMessage(Message msg) {
            switch (msg.what) {
                //开始定位
                case Utils.MSG_LOCATION_START:
                    break;
                // 定位完成
                case Utils.MSG_LOCATION_FINISH:
                    amapLocation = (com.amap.api.location.AMapLocation) msg.obj;
                    String result = Utils.getLocationStr(amapLocation);
                    if (amapLocation.getErrorCode() == 0) {
                        location.amapLocation(amapLocation);
                        Log.e("---------location", +amapLocation.getLatitude() + "---" + amapLocation.getLongitude());
                    }
                    break;
                //停止定位
                case Utils.MSG_LOCATION_STOP:
                    break;
                default:
                    break;
            }
        }

        ;
    };

    public interface getLocation {
        void amapLocation(com.amap.api.location.AMapLocation amapLocation);
    }

    // 根据控件的选择，重新设置定位参数
    private void initOption() {
        // 设置是否需要显示地址信息
        locationOption.setNeedAddress(true);
        locationOption.setOnceLocation(false);
        locationOption.setOnceLocationLatest(false);
        /**
         * 设置是否优先返回GPS定位结果，如果30秒内GPS没有返回定位结果则进行网络定位
         * 注意：只有在高精度模式下的单次定位有效，其他方式无效
         */
        locationOption.setGpsFirst(true);
        // 设置发送定位请求的时间间隔,最小值为1000，如果小于1000，按照1000算
        locationOption.setInterval(1000);
    }

    public void onDestroy(Context context) {
        if (null != locationClient) {
            locationClient.onDestroy();
            locationClient = null;
            locationOption = null;
        }
        if (null != alarmReceiver) {
            context.unregisterReceiver(alarmReceiver);
            alarmReceiver = null;
        }
        Log.e("tui", "tioito");
    }

    private BroadcastReceiver alarmReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("LOCATION")) {
                if (null != locationClient) {
                    locationClient.startLocation();
                }
            }
        }
    };

    /**
     * 定位成功后回调函数
     */
    @Override
    public void onLocationChanged(com.amap.api.location.AMapLocation amapLocation) {
        if (null != amapLocation) {
            Message msg = mHandler.obtainMessage();
            msg.obj = amapLocation;
            msg.what = Utils.MSG_LOCATION_FINISH;
            mHandler.sendMessage(msg);
        }
    }
}
